package com.superquan.selfstudybootmybatis.utils.httpapply;

import com.alibaba.fastjson.JSONObject;
import com.superquan.selfstudybootmybatis.consts.Constant;
import com.superquan.selfstudybootmybatis.entity.TraceUpdateOrderStatusCondition;
import com.superquan.selfstudybootmybatis.entity.Uce56Result;
import com.superquan.selfstudybootmybatis.utils.JsonUtil;
import com.superquan.selfstudybootmybatis.utils.MD5;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zwq
 * @description https工具类
 * @date 2022/4/14
 */
@Slf4j
public class HttpsUtil {

    private HttpsUtil() {
    }

    /**
     * post请求
     *
     * @param url
     * @param jsonStrParams
     * @return
     * @throws Exception
     */
//    public static String doPost(String url, String jsonStrParams) throws Exception {
//        HttpClient httpClient = null;
//        HttpPost httpPost = null;
//        String result = null;
//        HttpResponse response = null;
//
//        httpClient = new SSLClient();
//        httpPost = new HttpPost(url);// 创建httpPost；url:需要请求的url
//        httpPost.addHeader("Content-Type", "application/json");
//        StringEntity se = new StringEntity(jsonStrParams, Constant.Common.CHAR_SET_GBK);
//        se.setContentType("text/json");
//        se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
//        httpPost.setEntity(se);//放入需要传的值
//        response = httpClient.execute(httpPost);//发送请求 并接收返回值
//        if (response != null) {
//            HttpEntity resEntity = response.getEntity();
//            if (resEntity != null) {
//                result = EntityUtils.toString(resEntity, Constant.Common.CHAR_SET_UTF8);//将返回值转为String字符串
//            }
//        }
//
//        return result;
//    }
//
//    /**
//     * localhost：8091/message
//     *
//     * @param url    http:localhost：8080
//     * @return params 参数
//     * @throws ClientProtocolException
//     * @throws IOException
//     */
//    public static JSONObject doPost1 (String url, String params) throws Exception {
//        HttpClient httpClient = new DefaultHttpClient();
//        HttpPost httpPost = new HttpPost(url);
//        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
//        if (StringUtils.isNotBlank(params)) {
//            httpPost.setEntity(new StringEntity(params, "utf-8"));
//        }
//        HttpResponse httpResponse = httpClient.execute(httpPost);
//        // 请求和响应都成功了
//        HttpEntity entity = httpResponse.getEntity();// 调用getEntity()方法获取到一个HttpEntity实例
//        String response  = EntityUtils.toString(entity, "utf-8");// 用EntityUtils.toString()这个静态方法将HttpEntity转换成字符串,防止
//        JSONObject jsonObject =JSONObject.parseObject(response);
//        return jsonObject;
//    }
//
//    public static String doGet(String url) throws Exception {
//        String result = null;
//        HttpClient httpClient = new SSLClient();
//        RequestConfig requestConfig = RequestConfig.custom()
//                .setConnectTimeout(5000)   //设置连接超时时间
//                .setConnectionRequestTimeout(5000) // 设置请求超时时间
//                .setSocketTimeout(5000)
//                .setRedirectsEnabled(true)//默认允许自动重定向
//                .build();
//        HttpGet httpGet = new HttpGet(url);
//        httpGet.setConfig(requestConfig);
//        HttpResponse httpResponse = httpClient.execute(httpGet);
//        if (httpResponse != null) {
//            result = EntityUtils.toString(httpResponse.getEntity());//获得返回的结果
//        }
//        return result;
//    }

    /**
     * 请求第三方溯源接口
     * @param url
     * @param param
     * @return
     */
    public static Uce56Result doPostForTrace(String url, Object param) {

        PostMethod post = new PostMethod(url);
        try {
            post.setRequestEntity(new StringRequestEntity(JsonUtil.toJSONString(param),"application/json","UTF-8"));
            String timestamp = System.currentTimeMillis() + "";
            String clientId="TRACE";
            String secretKey = "WfTraceVxzPtl";
            String token = MD5.getMD5(  clientId + timestamp + secretKey);
            post.setRequestHeader("clientId", clientId);
            post.setRequestHeader("timestamp", timestamp);
            post.setRequestHeader("token", token);
            HttpClient client = new HttpClient();
            client.executeMethod(post);
            byte[] responseBody = post.getResponseBody();
            String body = new String(responseBody, StandardCharsets.UTF_8);
            return JsonUtil.parseJSONObject(body,Uce56Result.class);
        } catch (Exception ex) {
            Uce56Result uce56Result = new Uce56Result();
            uce56Result.setCode("-999");
            uce56Result.setMsg("接口请求异常！");
            return uce56Result;
        }
    }

    public static void main(String[] args) {
//        String url="http://192.168.18.37:8001/tail/interactive/updateOrderStatus";
        String url1="https://staging-qr.goodyear.com.cn/api/business-microservice/tail/interactive/updateOrderStatus";
        TraceUpdateOrderStatusCondition traceUpdateOrderStatusCondition=new TraceUpdateOrderStatusCondition();
        List<String> orderNo =new ArrayList<>();
        orderNo.add("W2204281106116539157");
        traceUpdateOrderStatusCondition.setOrderNo(orderNo);
        traceUpdateOrderStatusCondition.setChannel(1);
        traceUpdateOrderStatusCondition.setSecondLevelOrderStatus("24");
        Uce56Result uce56Result = doPostForTrace(url1, traceUpdateOrderStatusCondition);
        System.out.println(JsonUtil.toJSONString(uce56Result));
    }
}
